// pages/weather/weather.js
var app = getApp()//获取url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    iconList: [{
      icon: 'shop',
      color: 'red',
      badge: 0,
      name: '餐饮'
    }, {
      icon: 'recordfill',
      color: 'orange',
      badge: 0,
      name: '风景'
    }, {
      icon: 'read',
      color: 'yellow',
      badge: 0,
      name: '科教'
    }, {
      icon: 'service',
      color: 'olive',
      badge: 0,
      name: '生活服务'
    }, {
      icon: 'safe',
      color: 'cyan',
      badge: 0,
      name: '医疗'
    }, {
      icon: 'cart',
      color: 'blue',
      badge: 0,
      name: '购物'
    }, {
      icon: 'discoverfill',
      color: 'purple',
      badge: 0,
      name: '发现'
    }, {
      icon: 'questionfill',
      color: 'mauve',
      badge: 0,
      name: '帮助'
    }, {
      icon: 'commandfill',
      color: 'purple',
      badge: 0,
      name: '问答'
    }, {
      icon: 'brandfill',
      color: 'mauve',
      badge: 0,
      name: '版权'
    }],
    gridCol:3,
    now:'',
    address:'',
    latitude: 0, //首次加载维度
    longitude: 0, //首次加载的经度
    mapName: "", //选点的位置
  },
  moveToLocation() {
    let that = this
    wx.chooseLocation({
        success: function (res) {
            console.log(res.name);
            //赋值给data中的mapName
            that.setData({
                mapName: res.name
            })
        },
        //错误信息
        fail: function () {
            console.log(err);
        }
    })
},
  navgeo:function(e){
    console.log("事件长什么样子=",e)
    console.log("事件数据=",e.currentTarget.dataset)
    var title = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/geo/geo?title='+title,
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 页面加载获取当前定位位置为地图的中心坐标
  var _this = this;
  wx.getLocation({
   success(data) {
    if (data) {
     _this.setData({
      latitude: data.latitude,
      longitude: data.longitude,
      markers:[{
       id:0,
       latitude: data.latitude,
       longitude: data.longitude,
       iconPath: '/images/dingwei1.png',
       width: 32,
       height: 32
      }]
     });
    }
   }
  });
    var that = this
    // 1. 先获取用户的当前位置信息
    wx.getLocation({
      type: 'wgs84',// 地球坐标系
      success (res) {
        console.log(res)
        const latitude = res.latitude 
        const longitude = res.longitude
        // 2. 获取当前的结构化地址
        wx.request({
          url: 'https://restapi.amap.com/v3/geocode/regeo?parameters', //逆地理编码url
          data: {
            key: 'bd94443798009a9d75e89bae46f822b1',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
            const Component=res.data.regeocode.addressComponent
            that.setData({
              address:Component.province+Component.city+Component.district
            })
          }
        })
        // 3. 再获取当前的天气信息
        wx.request({
          url: 'https://devapi.qweather.com/v7/weather/now?', //仅为示例，并非真实的接口地址
          data: {
            key: 'f24409b043784067879a97242fb7def9',
            location: longitude+','+latitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            console.log(res.data)
            that.setData({
              now:res.data.now
            })
          }
        })

      }
     })
     

  },
  getType(e) {//获取选择的附近关键词，同时更新状态
    this.setData({ status: e.currentTarget.dataset.type})
    this.getAround(e.currentTarget.dataset.keywords,e.currentTarget.dataset.type);
   },
   getAround(keywords,types) {//通过关键词获取附近的点，只取前十个，同时保证十个点在地图中显示
    var _this = this;
    var myAmap = new amap.AMapWX({ key: key });
    myAmap.getPoiAround({
     iconPath: '/images/dingwei1.png',
     iconPathSelected: '/images/dingwei1.png',
     querykeywords: keywords,
     querytypes: types,
     location: _this.data.location,
     success(data) {
      if (data.markers) {
       var markers = [], points = [];
       for (var value of data.markers) {
        if (value.id > 9) break;
        if(value.id == 0){
         _this.setData({
          name: value.name,
          address: value.address,
          isShow: true
         })
        }
        markers.push({
         id: value.id,
         latitude: value.latitude,
         longitude: value.longitude,
         title: value.name,
         iconPath: value.iconPath,
         width: 32,
         height: 32,
         anchor: { x: .5, y: 1 },
         label: {
          content: value.name,
          color: 'green',
          fontSize: 12,
          borderRadius: 5,
          bgColor: '#fff',
          padding: 3,
          x: 0,
          y: -50,
          textAlign: 'center'
         }
        });
        points.push({
         latitude: value.latitude,
         longitude: value.longitude
        })
       }
       _this.setData({
        markers: markers,
        points: points
       })
      }
     },
     fail: function (info) {
      wx.showToast({title: info})
     }
    })
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})